import socket,sys,json

# 创建一个TCP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定IP地址和端口
server_socket.bind(('localhost', 8812))
n=2
# 开始监听连接
server_socket.listen(n)
print('Server started, waiting for connections...')
d={}

def datas(data):
    global d
    if len(d)>0:
        k=0
        for x in range(0,len(d)):
            if str(d[x])==str(data):
                k=1
            else:
                pass
        if k==0:   
            d[len(d)]=data
    else:
        d[0]=data

# 循环接受客户端连接
p=0
while True:
    try:
        c=""
        # 接受客户端连接
        client_socket, client_address = server_socket.accept()
        print(f'Connection from {client_address}')
        # 处理客户端数据
        data = client_socket.recv(1024)
        if not data:
            break
        print(f'Received: {data.decode()}')
        c=f'{data.decode()}'
        try:
            datas(c)
            if d:
                print(d)
        except Exception as e:
            pass
        # 发送响应数据
        # client_socket.sendall(b'Hello, client!')
        message=sys.stdin.readline()
        client_socket.sendall(message.encode())
    finally:
        client_socket.close()

